package com.zwps.biz.domain.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.model.dto.mointordentify.AlarmMessageDTO;
import com.zwps.biz.api.model.dto.mointordentify.PictureMessageDTO;
import com.zwps.biz.dal.db.dao.MonitorAlarmRecordDAO;
import com.zwps.biz.dal.db.dao.MonitorAlarmRecordIdentifyDAO;
import com.zwps.biz.dal.db.object.MonitorAlarmRecordDO;
import com.zwps.biz.dal.db.object.MonitorAlarmRecordIdentifyDO;
import com.zwps.biz.domain.converter.AlarmMessageConverter;
import com.zwps.biz.domain.enums.AlarmFlagEnum;
import com.zwps.biz.domain.enums.InvasionTypeEnum;
import com.zwps.biz.domain.service.MonitorAlarmRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class MonitorAlarmRecordServiceImpl extends ServiceImpl<MonitorAlarmRecordDAO, MonitorAlarmRecordDO>
        implements MonitorAlarmRecordService {

    @Autowired
    private MonitorAlarmRecordDAO monitorAlarmRecordDAO;

    @Autowired
    private MonitorAlarmRecordIdentifyDAO identifyDAO;
    @DS("lhk")
    @Transactional(rollbackFor = Exception.class)
    public void addRecord(AlarmMessageDTO alarmMessageDTO) {
        String type = String.valueOf(AlarmFlagEnum.getTypeByDescription(alarmMessageDTO.getAlarmFlag()));
        alarmMessageDTO.setAlarmFlag(type);
        MonitorAlarmRecordDO alarmRecordDO = AlarmMessageConverter.INSTANCE.toAlarmRecordDO(alarmMessageDTO);
        List<Integer> countList = null;
        List<String> nameList = null;
        if (alarmMessageDTO.getIdentifyData() != null) {
            countList = alarmMessageDTO.getIdentifyData().getSumup().getCount();
            nameList = alarmMessageDTO.getIdentifyData().getDetail().getName();

            if (countList != null) {
                String identifyNumber = countList.stream()
                        .map(String::valueOf)
                        .collect(Collectors.joining(","));
                alarmRecordDO.setIdentifyNumber(identifyNumber);
            } else {
                alarmRecordDO.setIdentifyNumber("");
            }
            if (null!=nameList ) {
                String identifyName = String.join(",", nameList);
                alarmRecordDO.setIdentifyName(identifyName);
            } else {
                alarmRecordDO.setIdentifyName("");
            }
            String identifyType = nameList.stream()
                    .map(s -> InvasionTypeEnum.fromName(s).getCode() + "")
                    .collect(Collectors.joining(","));
            alarmRecordDO.setIdentifyType(identifyType);
        }
        if ("2".equals(alarmRecordDO.getIdentifyType())) {
            // 当前日期
            LocalDate today = LocalDate.now();
            LocalDateTime startOfDay = today.atStartOfDay();
            LocalDateTime endOfDay = today.plusDays(1).atStartOfDay().minusNanos(1);

            List<MonitorAlarmRecordDO> records = monitorAlarmRecordDAO.selectList(
                    new LambdaQueryWrapper<MonitorAlarmRecordDO>()
                            .eq(MonitorAlarmRecordDO::getDeviceId, alarmRecordDO.getDeviceId())
                            .ge(MonitorAlarmRecordDO::getAlarmTime, startOfDay)  // 大于等于今天 00:00:00
                            .le(MonitorAlarmRecordDO::getAlarmTime, endOfDay)    // 小于等于今天 23:59:59
            );
            if (records.size() <= 0){

                monitorAlarmRecordDAO.insert(alarmRecordDO);
                addIdentify(nameList,countList,alarmMessageDTO);
            }
        }else {
            monitorAlarmRecordDAO.insert(alarmRecordDO);
            addIdentify(nameList,countList,alarmMessageDTO);

        }
    }
    public void addIdentify(List<String>  nameList ,List<Integer> countList, AlarmMessageDTO alarmMessageDTO) {
        if (null!=nameList && null!=countList ) {
            for (int i = 0; i < nameList.size(); i++) {
                MonitorAlarmRecordIdentifyDO identifyDO = AlarmMessageConverter.INSTANCE.toAlarmRecordIdentifyDO(alarmMessageDTO);
                identifyDO.setIdentifyName(nameList.get(i));
                identifyDO.setIdentifyType(InvasionTypeEnum.fromName(nameList.get(i)).getCode());
                identifyDO.setIdentifyNum(String.valueOf(countList.get(i)));
                identifyDAO.insert(identifyDO);
            }
        }
    }
    @DS("lhk")
    public void updateRecord(AlarmMessageDTO alarmMessageDTO) {
        String type = String.valueOf(AlarmFlagEnum.getTypeByDescription(alarmMessageDTO.getAlarmFlag()));
        alarmMessageDTO.setAlarmFlag(type);
        MonitorAlarmRecordDO alarmRecordDO = AlarmMessageConverter.INSTANCE.toAlarmRecordDO(alarmMessageDTO);
        UpdateWrapper<MonitorAlarmRecordDO> wrapper = new UpdateWrapper<>();
        wrapper.set("alarm_flag",alarmRecordDO.getAlarmFlag());
        wrapper.set("comment",alarmRecordDO.getComment());
        wrapper.set("alarm_time",alarmRecordDO.getAlarmTime());
        wrapper.eq("alarm_id",alarmRecordDO.getAlarmId());
        wrapper.eq("device_id",alarmRecordDO.getDeviceId());
        monitorAlarmRecordDAO.update(alarmRecordDO,wrapper);
    }
    @DS("lhk")
    @Override
    public void addRecordInit(AlarmMessageDTO alarmMessageDTO) {
        if ("BEGIN".equals(alarmMessageDTO.getAlarmFlag())) {
            addRecord(alarmMessageDTO);
        }
        if ("END".equals(alarmMessageDTO.getAlarmFlag())) {
        updateRecord(alarmMessageDTO);
        }

    }


}





